package cn.edu.sgu.www.mhxysy.controller.chongwu_equipment;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.chongwu_equipment.ChongwuEquipmentDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuEquipment;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.chongwu_equipment.ChongwuEquipmentService;
import cn.edu.sgu.www.mhxysy.vo.chongwu_equipment.ChongwuEquipmentVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "宠物装备管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/chongwu_equipment", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ChongwuEquipmentController {

	private final ChongwuEquipmentService chongwuEquipmentService;

	@Autowired
	public ChongwuEquipmentController(ChongwuEquipmentService chongwuEquipmentService) {
		this.chongwuEquipmentService = chongwuEquipmentService;
	}

	@ApiOperation("一键计算并修改宠物装备评分")
	@RequestMapping(value = "/compute", method = RequestMethod.POST)
	public JsonResult<Void> compute(
			@ApiParam(name = "id", value = "宠物装备ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String equipmentId) {
		chongwuEquipmentService.compute(equipmentId);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("穿戴宠物装备")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "chongwuId", value = "宠物ID", required = true),
			@ApiImplicitParam(name = "equipmentId", value = "宠物装备ID", required = true)
	})
	@RequestMapping(value = "/wear", method = RequestMethod.POST)
	public JsonResult<Void> wear(
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId,
			@NotBlank(message = CustomMessages.CHONGWU_EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		chongwuEquipmentService.wear(chongwuId, equipmentId);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("添加宠物装备")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ChongwuEquipmentDTO chongwuEquipmentDTO) {
		chongwuEquipmentService.insert(chongwuEquipmentDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除宠物装备")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "宠物装备ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		chongwuEquipmentService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改宠物装备信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ChongwuEquipmentDTO chongwuEquipmentDTO) {
		chongwuEquipmentService.updateById(chongwuEquipmentDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部宠物装备")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<ChongwuEquipment>> selectAll() {
		List<ChongwuEquipment> list = chongwuEquipmentService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询宠物装备信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<ChongwuEquipment> selectById(
			@ApiParam(name = "id", value = "宠物装备ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		ChongwuEquipment chongwuEquipment = chongwuEquipmentService.selectById(id);

		return JsonResult.success(null, chongwuEquipment);
	}

	@ApiOperation("查询宠物装备总评分")
	@RequestMapping(value = "/getTotalScore", method = RequestMethod.GET)
	public JsonResult<Integer> getTotalScore(
			@ApiParam(name = "equipmentId", value = "宠物装备ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		Integer score = chongwuEquipmentService.getTotalScore(equipmentId);

		return JsonResult.success(null, score);
	}

	@ApiOperation("查询宠物装备列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "宠物装备类型"),
			@ApiImplicitParam(name = "roleId", value = "角色ID"),
			@ApiImplicitParam(name = "chongwuId", value = "宠物ID")
	})
	@RequestMapping(value = "/selectList", method = RequestMethod.GET)
	public JsonResult<List<ChongwuEquipment>> selectList(
			@RequestParam(required = false) Integer type,
			@RequestParam(required = false) String roleId,
			@RequestParam(required = false) String chongwuId) {
		List<ChongwuEquipment> list = chongwuEquipmentService.selectList(type, roleId, chongwuId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("分页查询宠物装备列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<ChongwuEquipment>> selectByPage(Pager<ChongwuEquipment> pager) {
		Page<ChongwuEquipment> page = chongwuEquipmentService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

	@ApiOperation("查询宠物已穿戴的宠物装备")
	@RequestMapping(value = "/getWeardEquipments", method = RequestMethod.GET)
	public JsonResult<List<ChongwuEquipmentVO>> getWeardEquipments(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		List<ChongwuEquipmentVO> list = chongwuEquipmentService.getWeardEquipments(chongwuId);

		return JsonResult.success(null, list);
	}

}